﻿module ExtensionMethods
open System

type Array with
    static member Shuffle( arr:'a[] ) = 
        let mutable initialArray = arr.Clone() :?> 'a[]
        let endIdx = initialArray.Length-1
        let ran = new Random()
        for i in 0..(endIdx/2) do
            let ranIdx = ran.Next(0,endIdx)
            let buf = initialArray.[i]
            initialArray.[i] <- initialArray.[ranIdx]
            initialArray.[ranIdx] <- buf
        initialArray

    static member GenerateFilledWithDouble (length:int, maxSum:double) = 
        let ran = new Random()
        let rec genRandomList (restLen:int) (restSum:double) (acc)= 
            if restLen = 1 then
                restSum::acc
            else                
                let mutable var = ran.NextDouble()
                let koe = if var < 0.5 then var + 0.2 else var - 0.2
                let el = koe*restSum/(float length)
                if var > 0.5 then
                    genRandomList (restLen - 1) (restSum - el)  (el::acc)
                else
                    genRandomList (restLen - 1) (restSum - el)  ((-el)::acc)

        genRandomList length maxSum [] |>
        List.toArray |>
        Array.Shuffle

